import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @description: TODO
 * @author: Sarah
 * @modified By: Sarah
 * @date: Created in 2021/7/25 22:19
 * @version:v1.0
 */

@Configuration
@MapperScan("com.example.demo02.mysql")
public class MybatisPlusConfig implements MetaObjectHandler {
//    /**
//     * mybatis-plus SQL执行效率插件【生产环境可以关闭】
//     */
//    @Bean
//    @ConditionalOnProperty(prefix = "mybatis-plus", name = "performance", havingValue = "true")
//    public PerformanceInterceptor performanceInterceptor() {
//        return new PerformanceInterceptor();
//    }

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

//    @Bean
//    public ISqlInjector sqlInjector() {
//        return new LogicSqlInjector();
//    }


//    @Bean
//    public RequestContextListener requestContextListener() {
//        return new RequestContextListener();
//    }


    @Override
    public void insertFill(MetaObject metaObject) {
        //默认未删除
        setFieldValByName("isDeleted", "0", metaObject);
        //创建时间默认当前时间
        setFieldValByName("createdTime", System.currentTimeMillis(), metaObject);
     //   setFieldValByName("createdBy", UserInfoContext.getUserInfo().getId(), metaObject);

        setFieldValByName("updatedTime", System.currentTimeMillis(), metaObject);
       // setFieldValByName("updatedBy", UserInfoContext.getUserInfo().getId(), metaObject);
    //    setFieldValByName("companyId", UserInfoContext.getUserInfo().getCurrentCompanyId(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {

        //创建时间默认当前时间
        setFieldValByName("updatedTime", System.currentTimeMillis(), metaObject);
        //创建人
//        setFieldValByName("updatedBy", UserInfoContext.getUserInfo() != null ? UserInfoContext.getUserInfo().getId() : "", metaObject);
//        setFieldValByName("companyId", UserInfoContext.getUserInfo()!= null ? UserInfoContext.getUserInfo().getCurrentCompanyId(): "", metaObject);
    }


}

